/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author karenAndy
 */
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class parser {

    public static List parseList(String lista) throws Exception {

        List objs = new LinkedList();
        int cont = 0;
        Scanner scn = new Scanner(prepare(lista));
        String cache;

        if (!scn.next().equals("(")) {
            throw new Exception();
        }
        do {
            cache = scn.next();

            if (!cache.equals(")")) {

                if (cache.equals("(")) {
                    cont++;
                    String tmp = "( ";
                    do {
                        tmp = scn.next();
                        cache = cache + " " + tmp;

                        if (tmp.equals("(")) {
                            cont++;
                        }
                        if (tmp.equals(")")) {
                            cont--;
                        }
                        cache = cache + " " + tmp;
                    } while (!tmp.equals(")") || cont != 0);

                    objs.add(parseList(cache));

                    //objs.add(parse(cache));

                } else {

                    objs.add(cache);
                }
            }
        } while (!cache.equals(")"));

        return objs;

    }

    public static String prepare(String data) {

        String cache = "";

        for (int veces = 0; veces < data.length(); veces++) {

            if (data.charAt(veces) == '(') {

                cache = cache + " ( ";
            } else if (data.charAt(veces) == ')') {

                cache = cache + " ) ";
            } else {

                cache = cache + data.charAt(veces);
            }
        }
        return cache;
    }
}
